package com.designpatterns.observer.classto;

import com.designpatterns.observer.interfaceto.Observer;
import com.designpatterns.observer.interfaceto.Subject;

import java.util.ArrayList;
import java.util.List;

/**
 * 3D服务号的实现
 */
public class ObjectFor3D implements Subject {

   private List<Observer> observerList = new ArrayList<>();

    /**
     * 3D彩票的号码
     * @param observer
     */
    private String msg;

    @Override
    public void registerObserver(Observer observer) {
        observerList.add(observer);
    }

    @Override
    public void removeObserver(Observer observer) {
        if (observerList.indexOf(observer) >= 0) {
            observerList.remove(observer);
        }

    }

    @Override
    public void notifyObservers() {
        for (Observer server:observerList) {
            server.update(msg);
        }
    }


    /**
     * 主体更新信息
     * @param msg
     */
    public void setMsg(String msg) {
        this.msg = msg;
        notifyObservers();
    }
}
